package com.lebang.service.ybd;

import com.alibaba.fastjson.JSON;
import com.lebang.component.RedisClient;
import com.lebang.domain.ybd.MemberGoldConfig;
import com.lebang.domain.ybd.MemberGoldRecord;
import com.lebang.domain.ybd.YbdCategoryConfig;
import com.lebang.domain.ybd.YbdCategoryMemberRecord;
import com.lebang.entity.RequestHeaders;
import com.lebang.example.ybd.MemberGoldConfigExample;
import com.lebang.example.ybd.YbdCategoryConfigExample;
import com.lebang.mapper.ybd.MemberGoldRecordMapper;
import com.lebang.mapper.ybd.YbdCategoryConfigMapper;
import com.lebang.mapper.ybd.YbdCategoryMemberRecordMapper;
import com.lebang.model.Whether;
import com.lebang.model.cences.YbdCategory;
import com.lebang.model.ybd.Contrast;
import com.lebang.service.BaseService;
import com.lebangweb.response.Callback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author: hwq
 * @Date: 2020/3/24 20:10
 * @Version 1.0
 */
@Service
public class CategoryConfigService extends BaseService<YbdCategoryConfig, Integer, YbdCategoryConfigExample> {
    @Autowired
    private YbdCategoryConfigMapper mapper;
    @Autowired
    private YbdCategoryMemberRecordMapper recordMapper;

    public List<CategoryModel> selectByCategory(YbdCategory category, RequestHeaders requestHeaders) {
        List<CategoryModel> models = new ArrayList<>();
        List<YbdCategoryConfig> ybdCategoryConfigs = this.selectListByCategory(category.name());
        LocalDate date = LocalDate.now();
        LocalDateTime dateTime = LocalDateTime.now();
        ybdCategoryConfigs.forEach(s -> {
            CategoryModel model = new CategoryModel();
            model.setRecordId(0);
            model.setPoint(s.getPoint());
            model.setStarTime(LocalDateTime.of(date, LocalTime.parse(s.getStarTime())));
            model.setEndTime(LocalDateTime.of(date, LocalTime.parse(s.getEndTime())));
            model.setTitle(s.getTitle());
            model.setSort(s.getSort());
            model.setConfigId(s.getConfigId());
            model.setIcon(s.getIcon());
            model.setCoolTime(s.getCoolTime());

            YbdCategoryMemberRecord record = recordMapper.selectByRequest(requestHeaders.getUserid(),
                    category.name(), s.getConfigId(), date);
            if (record == null) {
                if (dateTime.isBefore(model.getStarTime()) || dateTime.isAfter(model.getEndTime())) {
                    //还未开始
                    if(model.getStarTime() .isAfter(dateTime)){
                        model.setStatus(-3);
                    }
                    //开始了但是没做 过期了
                    if(model.getEndTime() .isBefore(dateTime)){
                       model.setStatus(-2);
                    }
                } else {
                    //可做
                    model.setStatus(-1);
                }
             } else {
                model.setStatus(record.getStatus());
                model.setRecordId(record.getId());
                model.setPushTime(record.getPushTime());
                int days = (int) dateTime.until(record.getPushTime() , ChronoUnit.SECONDS);
                model.setCoolTime(Math.max(days, 0));
            }
            models.add(model);
        });
        return models;
    }

    /**
     * 根据类型获取配置
     *
     * @param category
     * @return
     */
    public List<YbdCategoryConfig> selectListByCategory(String category) {
        YbdCategoryConfigExample example = new YbdCategoryConfigExample();
        example.createCriteria().andCategoryEqualTo(category);
        example.setOrderByClause("sort ");
        return this.selectByExample(example);
    }

    /**
     * 查询该阶段的状态
     *
     * @param memberId
     * @param category
     * @param configId
     * @param date
     * @return
     */
    public boolean checkDayHas(String memberId, String category, String configId, LocalDate date) {
        YbdCategoryMemberRecord record = recordMapper.selectByRequest(memberId, category, configId, date);
        return record == null;
    }

    public YbdCategoryConfig selectByConfigId(String configId) {
        return mapper.selectByConfigId(configId);
    }

    public void createRecord(YbdCategoryConfig ybdCategoryConfig, RequestHeaders requestHeaders) {
        YbdCategoryMemberRecord record=new YbdCategoryMemberRecord();
        record.setHasDeleted(0);
        record.setConfigId(ybdCategoryConfig.getConfigId());
        record.setCreateTime(LocalDateTime.now());
        record.setModifyTime(LocalDateTime.now());
        record.setCategory(ybdCategoryConfig.getCategory());
        record.setMemberId(requestHeaders.getUserid());
        record.setAppStore(requestHeaders.getAppStore());
        record.setVersion(requestHeaders.getVersion());
        record.setIpAddr(requestHeaders.getIpAddr());
        record.setMacAddr(requestHeaders.getMacAddr());
        record.setImei(requestHeaders.getImei());
        record.setPoint(ybdCategoryConfig.getPoint());
        record.setStatus(0);
        record.setType(0);
        record.setCreateDate(LocalDate.now());
        record.setPushTime(LocalDateTime.now().plusSeconds(ybdCategoryConfig.getCoolTime()));
        recordMapper.insertSelective(record);

    }

    public YbdCategoryMemberRecord selectByRecordId(Integer recordId) {
        return recordMapper.selectByPrimaryKey(recordId);
    }

    public void pushRecord(Integer id, int i) {
        recordMapper.updateStatusById(id,i);
    }

    public List<YbdCategoryConfig> selectAllCategory() {
        return mapper.selectAllCategory();
    }
}
